package cn.edu.jsu.zjj.service.impl;

import cn.edu.jsu.zjj.constant.CommonConstant;
import cn.edu.jsu.zjj.exception.BaseException;
import cn.edu.jsu.zjj.mapper.UserCourseMapper;
import cn.edu.jsu.zjj.model.dto.usercourse.UserCourseQueryRequest;
import cn.edu.jsu.zjj.model.entity.User;
import cn.edu.jsu.zjj.model.entity.UserCourse;
import cn.edu.jsu.zjj.model.utils.SqlUtils;
import cn.edu.jsu.zjj.service.UserCourseService;
import cn.edu.jsu.zjj.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ly
 * @description 针对表【sys_userCourse(系统专业设置)】的数据库操作Service实现
 * @createDate 2024-11-09 10:17:12
 */
@Service
public class UserCourseServiceImpl extends ServiceImpl<UserCourseMapper, UserCourse>
        implements UserCourseService {
    @Autowired
    private UserCourseMapper userCourseMapper;
    @Autowired
    private UserService userService;

    @Override
    public QueryWrapper<UserCourse> getQueryWrapper(UserCourseQueryRequest userCourseQueryRequest) {
        if (userCourseQueryRequest == null) {
            throw new BaseException("请求参数为空");
        }
        Long id = userCourseQueryRequest.getId();
        User loginUser = userService.getLoginUser();
        String courseName = userCourseQueryRequest.getCourseName();
        String term = userCourseQueryRequest.getTerm();
        String sortField = userCourseQueryRequest.getSortField();
        String sortOrder = userCourseQueryRequest.getSortOrder();
        QueryWrapper<UserCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.eq( "create_user_id", loginUser.getCreateUserId());
        queryWrapper.like(StringUtils.isNotBlank(courseName), "course_name", courseName);
        queryWrapper.eq(StringUtils.isNotBlank(term), "term", term);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }
}


